<template>
    <van-button
        v-if="isFollow"
        class="follow-btn"
        round
        size="small"
        @click="onFollow"
        :loading="loading"
    >已关注</van-button>
    <van-button
        v-else
        class="follow-btn"
        type="info"
        color="#3296fa"
        round
        size="small"
        icon="plus"
        @click="onFollow"
        :loading="loading"
    >关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/article'
export default {
  name: '',
  components: {},
  model: {
    prop: 'isFollow',
    event: 'update-is_followed'
  },
  props: {
    isFollow: {
      type: Boolean,
      required: true
    },
    userId: {
      // [String, Number, Object]
      type: [String, Number, Object],
      required: true
    }
  },
  data () {
    return {
      followLoading: true,
      loading: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onFollow () {
      this.loading = true
      try {
        if (this.isFollow) {
          const res = await deleteFollow(this.userId)
          console.log(res)
        } else {
          const res = await addFollow(this.userId)
          console.log(res)
        }
        // this.article.is_followed = !this.article.is_followed
        this.$emit('update-is_followed', !this.isFollow)
      } catch (err) {
        console.log(err)
        let message = '操作失败请重试'
        if (err.response && err.response.status === 400) {
          message = '你不能关注你自己'
        }
        this.$toast(message)
      }
      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>

</style>
